#include "SplashManager.h"
#include <cocostudio/CocoStudio.h>
#include "../Menu/MenuManager.h"
using namespace cocostudio;


Scene* SplashManager::createScene()
{
    auto scene  = Scene::create();
    
    auto layer = SplashManager::create();
     
    scene->addChild(layer);
    return scene;
}

// on "init" you need to initialize your instance
bool SplashManager::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    setKeypadEnabled(true);
    
    m_screenSize = Director::getInstance()->getVisibleSize();
    initBackground();
    initalize();
    
    return true;
}
void SplashManager::initBackground()
{
    Sprite* background = Sprite::create("bg_intro.png");
    background->setPosition(m_screenSize.width/2,m_screenSize.height/2);
    float scaleX = m_screenSize.width/background->getContentSize().width;
    float scaleY = m_screenSize.height/background->getContentSize().height;
    background->setScale(scaleX, scaleY);
    this->addChild(background);
}
void SplashManager::initalize()
{
    
    auto node = CSLoader::getInstance()->createNode("SplashScene.csb");
    float scale = m_screenSize.height / node->getContentSize().height;
    
    node->setScale(scale);
    cocos2d::Vector<Node*> list = node->getChildren();
    node->setAnchorPoint(Vec2(0.5f,0.5f));
    node->setPosition(Vec2(m_screenSize.width / 2, m_screenSize.height / 2));
    
    //    list.at(0)->getChildByName("Designer")->setVisible(false);
    //    list.at(0)->getChildByName("CautionParent")->setVisible(false);
    
    cautionParentLayer = list.at(0)->getChildByTag(ESplashLayerTag::CautionParent);
    loadingLayer = list.at(0)->getChildByTag(ESplashLayerTag::Loading);
    designerLayer = list.at(0)->getChildByTag(ESplashLayerTag::Designer);
    
    cocos2d::ui::Button* btn = static_cast<cocos2d::ui::Button*>(cautionParentLayer->getChildByTag(ECautionParentLayerTag::Board)->getChildByTag(ECautionParentLayerTag::ButtonOK));

   
    btn->addClickEventListener(CC_CALLBACK_1(SplashManager::btnOKTouched, this));
    this->addChild(node);
    
    this->scheduleUpdate();
}
void SplashManager::btnOKTouched(Ref* pSender)
{
    Director::getInstance()->replaceScene(TransitionFade::create(1.0f,MenuManager::createScene()));
}
void SplashManager::update(float dt)
{
    cocos2d::ui::LoadingBar* loadingBar = static_cast<cocos2d::ui::LoadingBar*>(loadingLayer->getChildByTag(ELoadingLayerTag::ProcessbarBG)->getChildByTag(ELoadingLayerTag::ProcessBarFG));
    if(loadingBar->getPercent() != 100)
    {
        loadingBar->setPercent(loadingBar->getPercent() + 5);
    }
    else
    {
        auto gotoDesigner = CallFuncN::create(CC_CALLBACK_0(SplashManager::gotoDesignerLayer, this));
        auto gotoCaution = CallFuncN::create(CC_CALLBACK_0(SplashManager::gotoCautionParentLayer, this));
        
        this->runAction(Sequence::create(gotoDesigner,DelayTime::create(1.5f),gotoCaution, NULL));
        this->unscheduleUpdate();
    }
}

void SplashManager::gotoCautionParentLayer()
{
    //designerLayer->setVisible(false);
    
    
    Point p = cautionParentLayer->getPosition();
    cautionParentLayer->setPosition(p.x,m_screenSize.height*3/2);
    cautionParentLayer->setVisible(true);
    designerLayer->runAction(MoveTo::create(1.5f, Vec2(designerLayer->getPosition().x,-m_screenSize.height)));
    cautionParentLayer->runAction(MoveTo::create(0.5f, p));
    
}
void SplashManager::gotoDesignerLayer()
{
    
    designerLayer->setVisible(true);
    loadingLayer->setVisible(false);
    
}